package com.fy.common.model.mq;

import com.fy.common.enums.ClientType;
import com.fy.common.enums.DestinationType;
import com.fy.common.enums.JobType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class WebsocketMessage extends KafkaBaseMessage {
    /**
     * 目的地
     */
    private String destination;

    /**
     * 消息类型
     */
    private JobType jobType;

    /**
     * 消息内容
     */
    private Object data;

    /**
     * 响应说明
     */
    private String message;

    /**
     * 分页信息
     */
    private MyPage pageInfo;

    /**
     * 是否需要处理离线消息
     */
    private boolean needOffline;

    private Long jobId;

    private String[] userIds;

    /**
     * 消息发送到哪些客户端，默认发所有客户端
     */
    private List<ClientType> clientTypes=new ArrayList<>();

    /**
     * 目的地类型
     */
    private DestinationType destinationType;

    /**
     * 目的地后缀，存入的是userid或者channelId
     */
    private String destinationSuffix;

    /**
     * 离线消息过期时间 单位:秒 默认-1 不过期
     */
    @Builder.Default
    private Long expireTime = -1L;

    public List<String> getDestinations(){
        if(destinationType==null || StringUtils.isEmpty(destinationSuffix)){
            return Collections.singletonList(destination);
        }

        return clientTypes.stream().map(clientType -> {
            String prefix = destinationType.getValue();

            if(clientType==ClientType.PC){
                prefix="/pc".concat(prefix);
            }

            return prefix.concat(destinationSuffix);
        }).collect(Collectors.toList());

    }

}
